#include "DialogSetDirectory.h"

//(*InternalHeaders(DialogSetDirectory)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(DialogSetDirectory)
const long DialogSetDirectory::ID_TEXTCTRL1 = wxNewId();
const long DialogSetDirectory::ID_BUTTON1 = wxNewId();
const long DialogSetDirectory::ID_BUTTON2 = wxNewId();
const long DialogSetDirectory::ID_BUTTON3 = wxNewId();
//*)

BEGIN_EVENT_TABLE(DialogSetDirectory,wxDialog)
	//(*EventTable(DialogSetDirectory)
	//*)
END_EVENT_TABLE()

DialogSetDirectory::DialogSetDirectory(wxWindow* parent,Database* Database,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(DialogSetDirectory)
	Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
	SetClientSize(wxSize(350,298));
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxPoint(32,80), wxSize(184,32), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	Button1 = new wxButton(this, ID_BUTTON1, _("选择目录"), wxPoint(224,80), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	Button2 = new wxButton(this, ID_BUTTON2, _("提交"), wxPoint(64,200), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
	Button3 = new wxButton(this, ID_BUTTON3, _("关闭"), wxPoint(192,200), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
	DirDialog1 = new wxDirDialog(this, _("Select directory"), wxEmptyString, wxDD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxDirDialog"));

	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogSetDirectory::OnChooseDirectory);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogSetDirectory::OnSubmit);
	Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogSetDirectory::OnClose);
	//*)

    ThisDatabase = Database;
    SearchPath = wxString(((DBSqlite *)ThisDatabase)->GetSearchDirectory().c_str(),wxConvUTF8);;
    TextCtrl1->SetValue(SearchPath);

    bplog(LOG_LEVEL_DEBUG,"DialogSetDirectory:The path to search from stored in database is %s",(char *)SearchPath.char_str());
}

DialogSetDirectory::~DialogSetDirectory()
{
	//(*Destroy(DialogSetDirectory)
	//*)
}


void DialogSetDirectory::OnTextCtrl1Text(wxCommandEvent& event)
{
}

void DialogSetDirectory::OnSubmit(wxCommandEvent& event)
{
    string str;
    wxString title,confirm;

    str = (char *)SearchPath.char_str();
    ((DBSqlite *)ThisDatabase)->SetSearchDirectory(str);

    title = wxString::FromUTF8("success");
    confirm = wxString::FromUTF8("successfully set the directory");
    wxMessageBox(title,confirm,wxOK,this);
    bplog(LOG_LEVEL_DEBUG,"DialogSetDirectory:The submitting path is %s",(char *)SearchPath.char_str());
}

void DialogSetDirectory::OnClose(wxCommandEvent& event)
{
    this->Close();
}

void DialogSetDirectory::OnChooseDirectory(wxCommandEvent& event)
{
    wxString ThisPath;

    DirDialog1->ShowModal();
    ThisPath = DirDialog1->GetPath();
    TextCtrl1->SetValue(ThisPath);
    SearchPath = ThisPath;

    bplog(LOG_LEVEL_DEBUG,"DialogSetDirectory:The chosen current path is %s",(char *)ThisPath.char_str());
}
